package com.genITeam.ria.bl;

import java.util.ArrayList;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.genITeam.ria.dao.EventDAO;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.utility.Utility;
import com.genITeam.ria.vo.Events;
import com.genITeam.ria.vo.ResponseVo;

/**
 * @author      05030022
 *
 * @version     1.0
 */

public class EventBL {

	public EventBL() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(EventBL.class);

	/**
	 * getEventDetail return the XML in String Format.
	 * 
	 * @param none
	 * 
	 * @return String
	 * @throws NGFException
	 */

	public String getEventDetail() throws NGFException {
		Session session = null;
		Transaction tx = null;
		String personalMessageXML = "";
		try {
			logger.info("Start getEventDetail");
			EventDAO eventDAO = new EventDAO();

			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			ResponseVo responseVo = eventDAO.getEventDetail(session);
			ArrayList eventsResult = responseVo.getEventList();

			personalMessageXML = this.generateEventXML(eventsResult);

			tx.commit();

		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

		return personalMessageXML;
	}

	/**
	 * saveEvent return save the newly added and send to all the memebrs
	 * 
	 * @param none
	 * 
	 * @return Events
	 * @throws NGFException
	 */

	public void saveEvent(Events events) throws NGFException {

		EventDAO eventDAO = null;
		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start saveEvent");
			eventDAO = new EventDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			eventDAO.saveEvent(events, session);
			tx.commit();
		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	/**
	 * deleteEvent This function is used to delete the Events
	 * 
	 * @param Events
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void deleteEvent(Events events) throws NGFException {

		EventDAO eventDAO = null;

		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start deleteEvent");
			eventDAO = new EventDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			eventDAO.deleteEvent(events, session);
			tx.commit();
		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	/**
	 * generateEventXML Generate the XML from the collection Events found
	 * 
	 * @param ArrayList
	 *            eventsResult
	 * 
	 * @return String
	 * @throws NGFException
	 */

	public String generateEventXML(ArrayList eventsResult) throws NGFException {
		StringBuffer xml = new StringBuffer("");
		Events events = new Events();

		try {
			logger.info("Start generateEventXML");
			xml = new StringBuffer("<?xml version='1.0' encoding='UTF-8'?>\n");
			xml.append("<resultset>\n");
			for (int i = 0; i < eventsResult.size(); i++) {
				events = (Events) eventsResult.get(i);

				xml.append("<result>");
				xml.append("<id>" + events.getId() + "</id>");
				xml.append("<senderName>" + Utility.getStringValue(events.getMember().getUserName(),"")
						+ "</senderName>");
				xml.append("<title>" + Utility.getStringValue(events.getTitle(),"") + "</title>");
				xml.append("<eventDate>" + events.getEventDate()
						+ "</eventDate>");
				xml.append("<eventTime>" + events.getEventTime()
						+ "</eventTime>");
				xml.append("<location>" + Utility.getStringValue(events.getLocation(),"") + "</location>");
				xml.append("<city>" + Utility.getStringValue(events.getCity(),"") + "</city>");
				xml.append("<detail> <![CDATA[" + Utility.getStringValue(events.getDetail(),"")
						+ " ]]> </detail>");
				xml.append("</result>");
				xml.append("\n");

			}// end for

			xml.append("</resultset>");

			System.out.println(xml);

		} catch (Exception e) {
			System.out.println(e.toString());
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

		return xml.toString();
	}

}
